#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "string.h"
#include "UIfile.h"
#include "editor.h"
#include <qinputdialog.h>
#include <algorithm>
#include <QMessageBox>

int n=0;

bool UIfileCompare(UIfile* b, UIfile* a) {
    if (a->getFilePtr()->IsDir() && !b->getFilePtr()->IsDir()) {
        return false;
    }
    if (!a->getFilePtr()->IsDir() && b->getFilePtr()->IsDir()) {
        return true;
    }
    return a->getFilePtr()->name_.compare(b->getFilePtr()->name_) > 0;
}

MainWindow::MainWindow(FileSystem *file_system, QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    file_system_ = file_system;
    ui->setupUi(this);
    setPath("/");
    updateFileArea();
}

MainWindow::~MainWindow()
{
    delete ui;
}

//返回
void MainWindow::on_back_clicked()
{
    MainWindow::CFile=NULL;
    //返回上一层
    //修改路径
    if (path == "/") return;
    file_system_->OpenDir(File::GetParentDir(path.toStdString()));
    updateFileArea();
    setPath(QString::fromStdString(File::GetParentDir(path.toStdString())));
}

//打开
void MainWindow::on_open_clicked()
{
    if(MainWindow::CFile!=NULL)
        MainWindow::CFile->open();
}

//删除
void MainWindow::on_del_clicked()
{
    if(CFile!=NULL){
        file_system_->DeleteFile(*CFile->getFilePtr());
        CFile = NULL;
        updateFileArea();
    }
}

void MainWindow::muenDelete(){
    if(CFile!=NULL){
        file_system_->DeleteFile(*CFile->getFilePtr());
        CFile = NULL;
        updateFileArea();
    }
}

//新建
void MainWindow::on_newFile_clicked()
{
    bool ok;
    QString text = "123";
    QInputDialog input;
    text = input.getText(this, " ", "请输入名字：", QLineEdit::Normal, 0, &ok);
    while(text.toLocal8Bit().length()>3){
        QMessageBox::warning(this, "警告", "名字超限长", QMessageBox::Yes, QMessageBox::Yes);
        QInputDialog input;
        text = input.getText(this, " ", "请输入名字：", QLineEdit::Normal, 0, &ok);
    }
    if(ok && !text.isEmpty()){
        if(ui->type->currentText()=="目录"){
            File newFile(text.toStdString(), path.toStdString());
            newFile.attribute_ = 0xC;
            file_system_->NewDir(newFile);
        }else if(ui->type->currentText()=="文件"){
            QString type;
            QInputDialog input;
            type = input.getText(this, " ", "请输入文件类型：", QLineEdit::Normal, 0, &ok);
            while (type.toLocal8Bit().length()>2) {
                QMessageBox::warning(this, "警告", "名字超限长", QMessageBox::Yes, QMessageBox::Yes);
                QInputDialog input;
                type = input.getText(this, " ", "请输入文件类型：", QLineEdit::Normal, 0, &ok);
            }
            File newFile(text.toStdString(), path.toStdString());
            newFile.type_ = type.toStdString();
            file_system_->NewFile(newFile);
        }
        MainWindow::updateFileArea();
    }

}

//更新文件区域
void MainWindow::updateFileArea(){
    QWidget * arWidget = new QWidget; // 创建滚动显示区域幕布
    QGridLayout * l = new QGridLayout(arWidget); // 创建布局, 用于将控件嵌入幕布中
    QScrollArea * testar = ui->FileArea; // 创建滚动显示区域
    testar->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff) ;
    setPresent(file_system_->GetDiskUsedRatio() * 100);
    // 清空原有文件列表

   
    MainWindow::CFile = NULL;
    
    for (auto a : fileList) {
        delete a;
    }
    fileList.clear();
    
    // 更新文件列表
    for (auto &f: *file_system_->GetOpeningPathDirItems()) {
        if (f.IsEmpty()) continue;
        UIfile *uf = new UIfile(this, &f);
        fileList.push_back(uf);
    }
    
    
    // 排序文件，目录在前
    std::sort(fileList.begin(), fileList.end(), UIfileCompare);
    
    // 文件显示
    int i=0,j=0;
    for(auto a : fileList){
        a->setSize(MainWindow::size);
        l->addWidget(a,i,j,1,1);
        if(j < MainWindow::RowNum){
            j++;
        }else{
            i++;
            j=0;
        }
    }

    testar->setAlignment(Qt::AlignRight); // 设置滚动条位置
    testar->setWidget(arWidget); // 将设置好的幕布嵌入到滚动显示区域
}

//搜索
void MainWindow::on_search_clicked()
{
    string opening_path= file_system_->GetOpeningPath();
    if (ui->searchText->text().length() == 0) {
        return;
    }
    string path_name = ui->searchText->text().toStdString();
    
    // 若开头不是根目录则获取其绝对路径
    if (path_name[0] != '/') {
        string path = file_system_->GetOpeningPath();
        if (path[path.length()-1] != '/') {
            path += '/';
        }
        path_name = path + path_name;
    }
    
    if (path_name == "/") {
        this->setPath("/");
        file_system_->OpenDir("/");
        updateFileArea();
        return;
    }
    
    // 获取文件目录项指针
    File* file = file_system_->GetFileDirItemPtr(path_name);
    if (file == nullptr) {
        QMessageBox::information(this, 
            tr("提示"), 
            tr("文件不存在，请重新输入"),
            QMessageBox::Cancel, 
            QMessageBox::Cancel);
       return;
    }
    if (file->IsDir()) {
        this->setPath(QString::fromStdString(file->GetPathName()));
        file_system_->OpenDir(file->GetPathName());
        updateFileArea();
    }
    else {
        UIfile uifile(this, file);
        uifile.open();
        // 还原原路径
        file_system_->OpenDir(opening_path);
    }
}

//显示小
void MainWindow::on_small_clicked()
{
    MainWindow::RowNum = 4;
    MainWindow::size = 0;
    MainWindow::updateFileArea();
}

//显示中
void MainWindow::on_mid_clicked()
{
    MainWindow::RowNum = 3;
    MainWindow::size = 1;
    MainWindow::updateFileArea();
}

//显示大
void MainWindow::on_big_clicked()
{
    MainWindow::RowNum = 2;
    MainWindow::size = 2;
    MainWindow::updateFileArea();
}

//查看磁盘
void MainWindow::on_showDisk_clicked()
{
    t.setTable(QString::fromStdString(file_system_->GetAllCodesInDisk()));
    t.exec();
}

//选择文件
void MainWindow::Choosefile(UIfile *f){
    MainWindow::CFile=f;
}

//获取当前文件/夹
UIfile* MainWindow::Getfile(){
    return MainWindow::CFile;
}

//设置选中文件为空
void MainWindow::setFileNULL(){
    MainWindow::CFile=NULL;
}

//设置路径
void MainWindow::setPath(QString p){
    path = p;
    ui->path->setText(path);
}

//获取路径
QString MainWindow::getPath(){
    return MainWindow::path;
}

//获取文件列表
void MainWindow::setList(){
//    获取
    MainWindow::fileList.clear();
//    刷新
    MainWindow::updateFileArea();
}

//设置使用率
void MainWindow::setPresent(double p){
    MainWindow::present=p;
    ui->DiskUsage->setValue(p);
    //设置样式
    if(p>=90){
        ui->DiskUsage->setStyleSheet("QProgressBar {margin:5px;text-align: center;}QProgressBar::chunk {background-color: red;}");
    }else if(p>=80){
        ui->DiskUsage->setStyleSheet("QProgressBar {margin:5px;text-align: center;}QProgressBar::chunk {background-color: #ffd867;}");
    }else{
        ui->DiskUsage->setStyleSheet("QProgressBar {margin:5px;text-align: center;}QProgressBar::chunk {background-color: #56d265;}");
    }
}

